package com.tnt.jxls;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.UUID;

/**
 * 随机数工具类
 */
public class RandomUtil {

    private static Random random = new Random();

    private final static int DATA = 0x9fa5 - 0x4e00 + 1;

	public static void main(String[] args) {
		System.out.println(getRandomJianHan(10));
	}

	/**

	 * 获取指定长度随机简体中文

	 * @param len int

	 * @return String

	 */

	public static String getRandomJianHan(int len)

	{
		String ret = "";

		for (int i = 0; i < len; i++) {

			String str = null;

			int hightPos, lowPos; // 定义高低位

			Random random = new Random();

			hightPos = (176 + Math.abs(random.nextInt(39))); //获取高位值

			lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值

			byte[] b = new byte[2];

			b[0] = (new Integer(hightPos).byteValue());

			b[1] = (new Integer(lowPos).byteValue());

			try{

				str = new String(b, "GBk"); //转成中文

			} catch (UnsupportedEncodingException ex){
				ex.printStackTrace();
			}

			ret += str;

		}

		return ret;

}

    /**
     * 产生随机汉字(含繁体）
     **/
    public static char getRandomChinese() {

        return (char)(0x4e00 + random.nextInt(DATA));
    }

	/**
	 * 产生随机字母
	 */
	private static char getRandomCharacter() {
		switch ((int)(Math.random()*2)) {
			case 0:
				return (char)(Math.random()*26+'A');

			case 1:
				return (char)(Math.random()*26+'a');
		}
		return 0;
	}

	/**
	 * 获取某个区间的整形
	 *
	 * @param min
	 *            最小值
	 * @param max
	 *            最大值
	 * @return int
	 */
	public static int getRandomInt(int min, int max) {
		Random random = new Random();
		return random.nextInt(max) % (max - min + 1) + min;
	}

    /**
     * 产生10以内数字
     * @author zhuangxl
     * @date 2014-1-3
     * @version version1.0
     * @return
     */
	private static long getRandomNumber(){
	    long num = Math.round(Math.random()*10);
	    if(10==num){
	    	return num-1;
		} else {
	    	return num;
		}

    }

	/**
	 * 获得一个长度为n的包含字母和数字的字符串
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	public static String getRandomString(int n){
		StringBuffer sBuffer=new StringBuffer("");
		for (int i = 0; i < n; i++) {
			switch ((int)(Math.random()*2)) {
				case 0:
					sBuffer.append(getRandomCharacter());

				case 1:
					sBuffer.append(getRandomNumber());
			}
		}
		return sBuffer.toString();
	}

	/**
	 * 获得一个长度为n的随机汉字字符串
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	public static String getRandomChinese(int n){

		StringBuffer sBuffer=new StringBuffer("");

		for (int i=0; i< n; i++){
			sBuffer.append(getRandomChinese());
		}

		return sBuffer.toString();
	}

	/**
	 * 获得一个长度为n的随机字母字符串
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	public static String getRandomCharacter(int n){

		StringBuffer sBuffer=new StringBuffer("");

		for (int i=0; i< n; i++){
			sBuffer.append(getRandomCharacter());
		}

		return sBuffer.toString();
	}

	/**
	 * 获得一个长度为n的随机数字字符串
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	public static String getRandomNumber(int n){

		StringBuffer sBuffer=new StringBuffer("");

		for (int i=0; i< n; i++){
			sBuffer.append(getRandomNumber());
		}

		return sBuffer.toString();
	}

	/**
	 * 获得一个长度为n的随机整数
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	public static Long getRandomLong(int n){


		return Long.parseLong(getRandomNumber(n));
	}
	/**
	 * 随机生成男女
	 * @author zhuangxl
	 * @date 2014-1-3
	 * @version version1.0
	 * @return
	 */
	private static String getRandomSex() {
		switch ((int)(Math.random()*2)) {
			case 0:
				return "男";

			case 1:
				return "女";
		}
		return "男";
	}

}
